//
//  Twenty-fourViewController.m
//  Project_B
//
//  Created by dllo on 15/12/26.
//  Copyright © 2015年 ZY. All rights reserved.
//

#import "Twenty-fourViewController.h"
#import "ZYDetailWebViewController.h"


@interface Twenty_fourViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain)UITableView *tableV;
@property (nonatomic, retain)NSMutableArray *newsArray;
@property (nonatomic, strong)UIView *aview;
@property (nonatomic, strong)UILabel *label;
@property (nonatomic, strong)UIButton *backBtn;
@end

@implementation Twenty_fourViewController

- (void)viewWillAppear:(BOOL)animated
{
    _aview.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createView];
    self.view.backgroundColor = [UIColor whiteColor];
    

    //数组初始化
    self.newsArray = [NSMutableArray array];
    
    // 创建TableView
    [self createTableView];
    
    // 获取数据
    [self getData];
    
}



#pragma mark ** 创建view
- (void)createView
{
    _aview = [UIView new];
    _aview.backgroundColor = [UIColor redColor];
    [self.navigationController.view addSubview:_aview];
    [_aview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(@0);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, 104));
    }];
    _label = [UILabel new];
    _label.text = @"24小时要闻";
    _label.textAlignment = NSTextAlignmentCenter;
    _label.font = [UIFont systemFontOfSize:30];
    [_aview addSubview:_label];
    [_label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(_aview);
        make.size.mas_equalTo(CGSizeMake(200, 40));
        
    }];
    
    
    
    
    _backBtn = [UIButton new];
    [_backBtn setImage:[UIImage imageNamed:@"bobo_back@2x.png"] forState:UIControlStateNormal];
    [_backBtn addTarget:self action:@selector(backAction) forControlEvents:(UIControlEventTouchUpInside)];
    [_aview addSubview:_backBtn];
    [_backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_label.mas_top).offset(5);
        make.left.equalTo(@10);
        make.size.mas_equalTo(CGSizeMake(30, 30));
        
    }];
}

#pragma mark ** 返回
- (void)backAction
{
    _aview.hidden = YES;
    self.navigationItem.hidesBackButton = YES;
    [self.navigationController popToRootViewControllerAnimated:YES];
}

#pragma mark ** 下拉放大
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //刚开始y的偏移量初始值就是-264
    CGFloat y = scrollView.contentOffset.y;//加上导航栏高度,第一次是-200
    
    NSLog(@"%f",y);
    if (y >= 0 && y <= 40) {
        _label.font = [UIFont systemFontOfSize:30-y/5];
        [_label mas_updateConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(_aview);
            make.size.mas_equalTo(CGSizeMake(200, 40));
            
        }];
        [_aview mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.top.equalTo(@0);
            make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, 104 - y));
        }];
        [_aview layoutIfNeeded];
        [_tableV mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@0);
            make.top.equalTo(@(40-y));
            make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT));
        }];
      
        
        [_backBtn mas_updateConstraints:^(MASConstraintMaker *make) {
            
            make.top.equalTo(_label.mas_top).offset(5);
            make.left.equalTo(@10);
            make.size.mas_equalTo(CGSizeMake(30, 30));
            
        }];

        
    }
    if (y < 0) {
        _label.font = [UIFont systemFontOfSize:30];
        [_label mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(@20);
            make.left.offset(SCREEN_WIDTH/2-100);
            make.size.mas_equalTo(CGSizeMake(200, 40));
            
        }];
        [_aview mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.top.equalTo(@0);
            make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, 104));
        }];
        [_tableV mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@0);
            make.top.equalTo(@(40));
            make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT));
        }];
        
        
        
        [_backBtn mas_updateConstraints:^(MASConstraintMaker *make) {
            
            make.top.equalTo(_label.mas_top).offset(5);
            make.left.equalTo(@10);
            make.size.mas_equalTo(CGSizeMake(30, 30));
            
        }];
        
        
        
    }
    if (y > 40) {
        _label.font = [UIFont systemFontOfSize:17];
        [_label mas_updateConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(_aview);
            make.size.mas_equalTo(CGSizeMake(200, 40));
            
        }];
        [_aview mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.top.equalTo(@0);
            make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, 64));
        }];
        [_aview layoutIfNeeded];
        [_tableV mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@0);
            make.top.equalTo(@(0));
            make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT));
        }];
        NSLog(@"------");
        
        
        [_backBtn mas_updateConstraints:^(MASConstraintMaker *make) {
            
            make.top.equalTo(_label.mas_top).offset(5);
            make.left.equalTo(@10);
            make.size.mas_equalTo(CGSizeMake(30, 30));
            
        }];

        
        
        
        
        
        
        
        
    }
}

#pragma mark ** 创建tableView
- (void)createTableView
{
    //
//    _tableV = [[UITableView alloc] initWithFrame:CGRectMake(0, 40, SCREEN_WIDTH, SCREEN_HEIGHT)];
    _tableV = [UITableView new];
    //代理
    _tableV.delegate = self;
    _tableV.dataSource = self;
    //预估导读
    _tableV.estimatedRowHeight = 120;
    //注册
    [_tableV registerClass:[LH24HoursCell class] forCellReuseIdentifier:@"LH24HoursCell"];
        [_tableV registerClass:[LH24HoursFirstCell class] forCellReuseIdentifier:@"LH24HoursFirstCell"];
    [self.view addSubview:_tableV];
    [_tableV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.top.equalTo(@40);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT));
    }];
}

#pragma mark ** 获取数据
- (void)getData
{
    // 获取网址
    NSString *url = @"http://c.m.163.com/nc/article/list/T1429173683626/0-20.html";
    
    [ZYTools getUrl:url body:nil result:ZYJSON headerFile:nil success:^(id result) {
        // 创建字典接收解析出来的数据
        NSDictionary *dataDic = result;
        
        // 根据标示找到数组
        NSArray *tArray = [dataDic objectForKey:@"T1429173683626"];
        
        // 拆分数据
        for (NSDictionary *dic in tArray) {
            LH24HoursModel *news = [[LH24HoursModel alloc] init] ;
            [news setValuesForKeysWithDictionary:dic];
            
            // 存入数组
            [self.newsArray addObject:news];
        }
        
        // 重载数据
        [self.tableV reloadData];
        
    } failure:^(NSError *error) {
        
        
    }];

}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _newsArray.count;
}
// 判断cell种类
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 第一行
    if (0 == indexPath.row) {
        LH24HoursFirstCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LH24HoursFirstCell"];
        cell.model = _newsArray[indexPath.row];
        return cell;
    }
    
    // 其他
    LH24HoursCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LH24HoursCell"];
    cell.model = _newsArray[indexPath.row];
    
    return  cell;
}
// 根据cell种类决定cell高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 第一行大图
    if (0 == indexPath.row) {
        return 280;
    }
    
    // 正常
    return 120;
}
#pragma mark ** cell点击
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ZYDetailWebViewController *webViewVC = [[ZYDetailWebViewController alloc]init];
    webViewVC.docId= [_newsArray[indexPath.row] docid];
    webViewVC.replyCount = [_newsArray[indexPath.row] replyCount] ;
    _aview.hidden = YES;
    [self.navigationController pushViewController:webViewVC animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
